/* a_balance.do ***************************************************************

	Description:
	- Conduct Basic Balance Regression for Table 1
	- Attrition results for Table 2

******************************************************************************/
set more off
use "$stata_data_analysis/full_samp.dta", clear

gen ehomelang = homelang1 == "S"
local specs "fr_K1 fr_al fr_al_ran"
order *, sequential
replace dob = 13010 if dob < 8600

local controls hispanic female black white pov ehomelang dob sp_biling pop ///
	is_biling age_PreK_enr med_inc_hh med_inc_fam med_gross_rent share_black ///
	share_white share_hispanic frpl num_ranked first_walk East

* Specification
local instrument 	soffany
local endog			att
local controls		`controls'
local other_controls "i.yr_asgn"
local psc			"pformany"
local restriction	"in_K1"
local suffix		"an_K1"


preserve

*Summary statistics
estpost su `controls' if `restriction'
esttab using "$results/tables/full_sum_stat`suffix'.csv", main(mean) aux(sd) replace

estpost su `controls' if !inlist(`psc',0,1) & `restriction'
esttab using "$results/tables/risk_sum_stat`suffix'.csv", main(mean) aux(sd) replace

*Balance without pscore
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') other_controls(`other_controls') ///
	outfile("$results/tables/bal_`suffix'") balance(1) non_offered_mean(1)

*Balance with pscore
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
	outfile("$results/tables/bal_cntrl`suffix'") balance(1) non_offered_mean(1)

* Balance with linear pscore
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
	outfile("$results/tables/bal_cntrl`suffix'_lin_cntrl") balance(1) non_offered_mean(1) lin_cntrl(1)

* Add FRPL as covariate, but conditional on being in sims
local restriction    "in_K1 & in_sims"
local controls 		 "frpl"

	* With no risk controls
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') other_controls(`other_controls') ///
	outfile("$results/tables/bal_`suffix'_frpl") balance(1) non_offered_mean(1)

	* with linear pscore controls
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') restriction(`restriction') pscore(`psc') other_controls(`other_controls') ///
	outfile("$results/tables/bal_cntrl`suffix'_lin_cntrl_frpl") balance(1) non_offered_mean(1) lin_cntrl(1)

restore



/********************** Attrition *********************************************/

use "$stata_data_analysis/full_samp.dta", clear
replace nsc_prim_name_link = 0 if nsc_prim_name_link == .
replace in_sims = 0 if in_sims == .
replace total_mcas = 0 if total_mcas == .
replace took_mcas = 0 if took_mcas == .

order *, sequential

* Specification
local instrument 	soffany
local endog			att
local controls		nsc_prim_name_link in_sims total_mcas took_mcas
local other_controls "i.yr_asgn"
local psc			"pformany"
local restriction	"in_K1"
local suffix		"an_K1"
local demographic_list "black white hisp male fem"


preserve
get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	balance_vars(`controls') pscore(`psc') restriction(`restriction') ///
	outfile("$results/tables/bal_att_`suffix'") balance(1) non_offered_mean(1)
restore
